package edu.xhu.communityantiepidemicsystemback.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import edu.xhu.communityantiepidemicsystemback.common.result.PageResult;
import edu.xhu.communityantiepidemicsystemback.common.result.Result;
import edu.xhu.communityantiepidemicsystemback.dto.*;
import edu.xhu.communityantiepidemicsystemback.entity.Community;
import edu.xhu.communityantiepidemicsystemback.entity.Notice;
import edu.xhu.communityantiepidemicsystemback.service.INoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcx
 * @since 2022-06-20
 */
@RestController
@RequestMapping("/notice")
@Slf4j
public class NoticeController {
    @Autowired
    private INoticeService noticeService;

    @GetMapping("/noticeList")
    public PageResult<?> getNoticeList(Integer pageNO, Integer pageSize){
        Query query = new Query();
        query.setPageNO(pageNO);
        query.setPageSize(pageSize);
        IPage<Notice> noticePage = noticeService.selectNoticeList(query);
        if(Objects.isNull(noticePage)){
            return new PageResult<>();
        }
        return new PageResult<Notice>(noticePage.getTotal(),noticePage.getRecords());
    }

    @PostMapping("/fuzzysearch")
    public PageResult<?> fuzzyNoticeInfo(@RequestBody Fuzzy fuzzy){
        IPage<Notice> noticeIPage= noticeService.fuzzyNoticeInfo(fuzzy);
        if(Objects.isNull(noticeIPage)){
            return new PageResult<>();
        }
        return new PageResult<>(noticeIPage.getTotal(),noticeIPage.getRecords());
    }

    @PostMapping("/add")
    public Result<?> addNotice(@RequestBody CreateNotice createNotice){
        boolean result = noticeService.addNotice(createNotice);
        if (result){
            return new Result<>().success();
        }else {
            return new Result<>().error("添加失败");
        }
    }

    @GetMapping("/del")
    public Result<?> delNotice(Integer id){
        boolean result = noticeService.delNotice(id);
        if (result){
            return new Result<>().success();
        }else {
            return new Result<>().error("删除失败");
        }
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody UpdateNotice updateNotice){
        boolean result = noticeService.updateNotice(updateNotice);
        if (result){
            return new Result<>().success();
        }else {
            return new Result<>().error("更新失败");
        }
    }


}
